package com.example.kindergarten.exceptionHandler

import com.example.kindergarten.util.ResultResponse
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.boot.context.properties.bind.BindException
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.MethodArgumentNotValidException
import org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.ResponseBody
import org.springframework.web.bind.annotation.ResponseStatus
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import javax.validation.ValidationException


/**
 * @description: 自定义异常处理
 * @author: DT
 * @date: 2021/4/19 21:51
 * @version: v1.0
 */
@ControllerAdvice
class GlobalExceptionHandler {
    private val logger: Logger = LoggerFactory.getLogger(ResultResponse::class.java)

    /**
     * 处理自定义的业务异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = [BizException::class])
    @ResponseBody
    fun bizExceptionHandler(req: HttpServletRequest?, e: BizException): ResultResponse {
        logger.error("状态码:${e.errorCode},错误：${e.message}")
        return ResultResponse.error(e.errorCode, e.errorMsg)
    }

    /**
     * 处理空指针的异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = [NullPointerException::class])
    @ResponseBody
    fun exceptionHandler(req: HttpServletRequest?, e: NullPointerException?): ResultResponse {
        logger.error("发生空指针异常！原因是:", e)
        return ResultResponse.error(ExceptionEnum.BODY_NOT_MATCH)
    }

    /**
     * 方法参数校验
     */
    @ExceptionHandler(value = [MethodArgumentNotValidException::class])
    fun exceptionHandler(req: HttpServletRequest, e: MethodArgumentNotValidException): ResponseEntity<ResultResponse> {
        logger.error(e.bindingResult.fieldError?.defaultMessage)
        var errInfo = ResultResponse.error(
            ExceptionEnum.BODY_NOT_MATCH.resultCode,
            e.bindingResult.fieldError?.defaultMessage
        )
        return ResponseEntity.status(HttpStatus.BAD_REQUEST)
            .body(errInfo)
    }


    /**
     * ValidationException
     */
    @ExceptionHandler(value = [ValidationException::class])
    fun exceptionHandler(e: ValidationException): ResultResponse {
        logger.error(e.message, e)
        return ResultResponse.error(ExceptionEnum.BODY_NOT_MATCH)
    }

    /**
     * 处理其他异常
     * @param req
     * @param e
     * @return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(AjaxResult.error());
     */
    @ExceptionHandler(value = [Exception::class])
    @ResponseBody
    fun exceptionHandler(req: HttpServletRequest?, e: Exception?): ResponseEntity<ResultResponse> {
        var errInfo: ResultResponse = if (e != null) {
            logger.error("发生业务异常！原因是：{}", e.message)
            ResultResponse.error(ExceptionEnum.INTERNAL_SERVER_ERROR.resultCode, e.message)
        } else {
            ResultResponse.error(ExceptionEnum.BODY_NOT_MATCH)
        }

        println("有异常错误")
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errInfo)
    }


}
